package cn.cocho.custom.yunCashierDf.merchant.service;

import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantCount;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantTransactionInfo;
import cn.cocho.dborm.core.Dborm;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WQJ on 2017-05-24.
 */
@Service("merchantTransactionInfoService")
public class MerchantTransactionInfoService {

    public JQGridPage getListByJQgrid(JQGridPage<MerchantTransactionInfo> pageJQGrid) {
        pageJQGrid = JQGridUtils.getListByJQgrid(pageJQGrid, MerchantTransactionInfo.class, "viewmerchantorder");
        for (MerchantTransactionInfo merchantTransactionInfo : pageJQGrid.getDataRows()) {
             merchantTransactionInfo.init();
        }
        return pageJQGrid;
    }
    public List<MerchantCount> getMerchantCount(String str,String[] obj) {
        String sql = "SELECT UUID() AS transaction_id, c1.codename,a.respcd AS respcd ,SUM(txamt+0)/100 AS type_num, a.corp_id AS corp_id,a.record_status AS record_status,a.create_user_id AS create_user_id," +
                "a.create_time AS create_time,a.last_modify_user_id AS last_modify_user_id,a.last_modify_time AS last_modify_time  FROM merchant_transaction_info  a LEFT JOIN codelist_info c1 ON (c1.codevalue = a.respcd AND c1.code_type_id = 'RY_JYJG_DM')";
       if(str!=""){
           sql+=" where  ";
           sql+=str;
       }
        sql +=" GROUP BY a.respcd ";
        return  Dborm.getEntities(sql,obj, MerchantCount.class);
    }
    public List<MerchantTransactionInfo> getMerchantTransactionInfoExcel(String str,String[] obj) {
        String sql = "select * from viewmerchantorder ";
        if(str!=""){
            sql+=" where  ";
            sql+=str;
        }
        sql+=" ORDER BY last_modify_time DESC " ;
        return  Dborm.getEntities(sql,obj, MerchantTransactionInfo.class);
    }
    public void update(MerchantTransactionInfo merchantTransactionInfo) {
        Dborm.replace(merchantTransactionInfo);
    }

    public void add(MerchantTransactionInfo merchantTransactionInfo) {
        merchantTransactionInfo.setTransactionId(StringUtils.getUUID());
        Dborm.insert(merchantTransactionInfo);
    }

    public MerchantTransactionInfo getMerchantTransactionInfoById(String transactionId) {
        MerchantTransactionInfo merchantTransactionInfo = new MerchantTransactionInfo();
        merchantTransactionInfo.setTransactionId(transactionId);
        return Dborm.getEntityByExample(merchantTransactionInfo, true);
    }

    public boolean insertMerchantTransactionInfo(MerchantTransactionInfo merchantTransactionInfo) {
        merchantTransactionInfo.setTransactionId(StringUtils.getUUID());
        return Dborm.insert(merchantTransactionInfo);
    }

    public boolean replaceMerchantTransactionInfo(MerchantTransactionInfo merchantTransactionInfo) {
        merchantTransactionInfo.setTransactionId(merchantTransactionInfo.getTransactionId());
        System.out.println(merchantTransactionInfo.getTransactionId()+"交易ID");
        return Dborm.replace(merchantTransactionInfo);
    }

    public List<MerchantTransactionInfo> getMerchantTransactionInfo() {
        String sql = "SELECT * FROM merchant_transaction_info ORDER BY transaction_id ASC";
        return Dborm.getEntities(sql, null, MerchantTransactionInfo.class);
    }

    public MerchantTransactionInfo getMerchantTransactionInfo(String merchantId,String origOrderNum) {
        String sql = "SELECT * FROM merchant_transaction_info where merchant_id = '"+merchantId+"' and orig_order_num = '"+origOrderNum+"'";
        return Dborm.getEntity(sql, null, MerchantTransactionInfo.class);
    }

    //定时任务需要的订单信息
    public MerchantTransactionInfo getQuMerchantTransactionInfo(String merchantId,String origOrderNum) {
        String sql = "SELECT * FROM merchant_transaction_info where merchant_id = '"+merchantId+"' and orig_order_num = '"+origOrderNum+"' and respcd = '09'";
        return Dborm.getEntity(sql, null, MerchantTransactionInfo.class);
    }

    public MerchantTransactionInfo getMerchantTransactionInfoCall(String origOrderNum,String terminalid) {
        String sql = "SELECT * FROM merchant_transaction_info where orig_order_num = '"+origOrderNum+"' and terminalid = '"+terminalid+"'" ;
        return Dborm.getEntity(sql, null, MerchantTransactionInfo.class);
    }

    //获取超时未支付的订单信息
    public List<MerchantTransactionInfo> getTimeOutTransactionInfo() {
        String sql = "SELECT * FROM merchant_transaction_info where respcd = '09' and (select TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(create_time/1000),NOW())) > 120 " +
                "and search_status = '0' AND terminalid NOT IN (SELECT terminalid FROM electricity_terminalid_info) ORDER BY transaction_id ASC";
        return Dborm.getEntities(sql, null, MerchantTransactionInfo.class);
    }

    //获取电商订单信息
    public List<MerchantTransactionInfo> getElectricityTransactionInfo() {
        String sql = "SELECT * FROM merchant_transaction_info WHERE respcd = '09' AND terminalid IN (SELECT terminalid FROM electricity_terminalid_info) ORDER BY transaction_id ASC";
        return Dborm.getEntities(sql, null, MerchantTransactionInfo.class);
    }
}
